package com.jphotomanager.io;

import java.awt.image.BufferedImage;
import java.io.File;

import org.apache.commons.io.FileUtils;

import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifIFD0Directory;

public class RawFileReader {

    public BufferedImage readPreviewImage(File file) throws Exception {
        Metadata metadata = ImageMetadataReader.readMetadata(file);
        ExifIFD0Directory directory = metadata
                .getDirectory(ExifIFD0Directory.class);
        String tagName = directory.getString(ExifIFD0Directory.TAG_MAKE);
        if (tagName.toLowerCase().equals("canon")) {
            int stripOffsets = directory.getInt(0x0111);
            int stripByteCounts = directory.getInt(0x0117);
            byte[] readFileToByteArray = FileUtils.readFileToByteArray(file);
            byte[] preview = new byte[stripByteCounts];
            System.arraycopy(readFileToByteArray, stripOffsets, preview, 0,
                    stripByteCounts);
            FileUtils.writeByteArrayToFile(new File(file.getParent(), file
                    .getName().replace(".CR2", ".jpg")), preview);
        } else if (tagName.toLowerCase().equals("nikon corporation")) {
            
        }
        return null;
    }

}
